var testid;
var testName;
var partNum;
var teacherName;
$(document).ready(function(){
	testid=getQueryString("test_id");
	$.ajax({
		url:"../reloadTestInf",
		type:"post",
		data:{
			testid:testid,
			partNo:0,
			questionNo:0,
			type:1,
		},
		async: false,
		success:function(res){
			testName=res.testName;
			partNum=res.partNum;
		}
	});	
	$.ajax({
		url:"../getCompletion",
		type:"post",
		data:{
			testid:testid,
			studentid:0,
			type:3,
		},
		async: false,
		success:function(res){
			teacherName=res.teacherName;
		}
	});
	addCompletions(testid);
});

function getQueryString(name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]);
	    return null;
}

function addCompletions(testid){
	$.ajax({
		url:"../getCompletion",
		type:"post",
		data:{
			testid:testid,
			studentid:0,
			type:1,
		},
		async: false,
		success:function(res){
			for(var i=1;i<=res.count;i++){
				var isFinished="";
				var checkBtn="";
				if(res["isFinished"+i]==0){
					isFinished="未完成"
				}else {
					isFinished="已完成";
					checkBtn=setCheckBtn(testid,res["studentId"+i]);
		        }
				
				$("#completionPart").append("<tr><th></th><th><p align=\"center\" id=\"testName\">"+testName+"</p></th><th><p align=\"center\" id=\"teacherName\">"+teacherName+"</p></th><th><p align=\"center\" id=\"partNum\">"+partNum+"</p></th><th><p align=\"center\" id=\"studentName\">"+res["studentName"+i.toString()]+"</p></th><th><p align=\"center\" id=\"completion\">"+isFinished+"</p></th><th><p align=\"center\">"+checkBtn+"</p></th><th></th></tr>");

			}
		}
	});	
}

function setCheckBtn(testid,studentid){	
	var btn="";
	$.ajax({
		url:"../getCompletion",
		type:"post",
		data:{
			testid:testid,
			studentid:studentid,
			type:2,
		},
		async: false,
		success:function(res){
			if(res.isFinished==0){
				btn="<a class=\"btn btn-small btn-warning\" href=\"auditStudentTest.jsp?test_id="+testid+"&studentid="+studentid+"\">批阅</a>";
			}else if(res.isFinished==1){
				btn="<a class=\"btn btn-small btn-primary\" href=\"viewStudentTest.jsp?test_id="+testid+"&studentid="+studentid+"\">查看</a>";
			}
		}
	});	
	return btn;
}